package com.jnsw.testkotlin

import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.Button
import android.widget.ImageView
import android.widget.RelativeLayout
import com.esri.android.map.GraphicsLayer
import com.esri.android.map.MapView
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer
import com.esri.android.runtime.ArcGISRuntime
import com.esri.core.geometry.GeometryEngine
import com.esri.core.geometry.Point
import com.esri.core.geometry.SpatialReference
import com.esri.core.map.Graphic
import com.esri.core.symbol.SimpleFillSymbol
import com.esri.core.symbol.SimpleMarkerSymbol
import kotlinx.android.synthetic.main.activity_view_stub_avtivity.*


class ViewStubAvtivity : AppCompatActivity() {
    var map : MapView?= null;
    var imageView : ImageView?= null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_stub_avtivity)


        val vs_btn = findViewById(R.id.vs_btn) as Button;
        val stub = findViewById(R.id.vs_vs) as ViewStub
        vs_btn.setOnClickListener { v: View? ->
            if (stub.parent!= null) {
                stub.inflate();
                map  = findViewById(R.id.map) as MapView;
                map!!.setBackgroundColor(Color.WHITE);
//                val layer =  ArcGISDynamicMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");
                val layer =  ArcGISDynamicMapServiceLayer("http://10.80.4.8:6080/arcgis/rest/services/影像发布测试/MapServer");
//                val layer =  ArcGISTiledMapServiceLayer("http://cache1.arcgisonline.cn/ArcGIS/rest/services/ChinaOnlineStreetColor/MapServer");
//                val layer =  ArcGISImageServiceLayer("http://10.80.4.8:6080/arcgis/rest/services/北海道街道名地址/MapServer",null);
//                ArcGISRuntime.setClientId("aQwcl5TiAFV7tK62")
                ArcGISRuntime.setClientId("aQscd4EsAVG7sg53")
                val mGraphicsLayer_note = GraphicsLayer();
                map!!.addLayer(layer);
                map!!.addLayer(mGraphicsLayer_note);
                map!!.setOnSingleTapListener { fl, fl1 ->
                    var p  = map!!.toMapPoint(fl,fl1);
                    val wgs = GeometryEngine.project(p, SpatialReference.create(2344), SpatialReference.create(4326)) as Point//坐标转换 失败

              var       fillSymbol     =   SimpleFillSymbol(Color.WHITE);
                    fillSymbol.setAlpha(40)// 设置透明度
                    // 画点
                    val sms = SimpleMarkerSymbol(Color.BLUE, 10,
                            SimpleMarkerSymbol.STYLE.CIRCLE)// 画多边形时的 点 的类型颜色 大小 类型


                    val graphic = Graphic(p, sms) // 根据坐标画点

                    mGraphicsLayer_note.addGraphic(graphic)// 图层上添加点

                    UpdateAnimationToMap(map!!,p);
                }
            }
        }
        vs_btn1.setOnClickListener { v ->

            vs_ll.visibility = View.VISIBLE

        }
    }


    fun addAnimationToMap(map: MapView, animationResourceId: Int,
                          geoPoint: Point) {
        Log.d("gps", "addview")

        if (imageView == null) {
//            map.centerAt(geoPoint, true)// 漫游到当前位置
//            val cp = map.center
            val ptCenter = map.toScreenPoint(geoPoint)
            //            int x = (int) (map.getScale() / 4900);

            val bl = 50
            val iLeft = ptCenter.x.toInt() - bl
            val iBottom = ptCenter.y.toInt() + bl
            val iRight = ptCenter.x.toInt() + bl
            val iTop = ptCenter.y.toInt() - bl
            imageView = ImageView(map.context)
            imageView!!.setImageResource(animationResourceId)
            val layoutParams = ViewGroup.LayoutParams(
                    100, 100)
            val rllayoutParams = RelativeLayout.LayoutParams(
                    layoutParams)
            rllayoutParams.setMargins(iLeft, iTop, 0, 0)
             imageView!!.setLayoutParams(rllayoutParams)
            val searchdiv = findViewById(R.id.RelativeLayout01) as RelativeLayout

            searchdiv.addView( imageView!!)
            // map.addView( imageView!!);
        }
         imageView!!.post(Runnable {
             val animationDrawable :AnimationDrawable = imageView!!.getDrawable() as AnimationDrawable;
             animationDrawable.start();
        })
    }

    fun UpdateAnimationToMap(map: MapView, geoPoint: Point) {
        if ( imageView ==null) {
                addAnimationToMap(map, R.drawable.gps_location, geoPoint)
        } else {
            //            int x = (int) (map.getScale() / 4900);

            val bl = 50
            Log.d("gps", "updateview")
            //            Log.d("test x", map.getScale() + "-------x" + x + "-----bl---" + bl);
            val ptCenter = map.toScreenPoint(geoPoint)
            Log.i("diandiandian",""+ptCenter.x+"------"+ptCenter.y);
            Log.i("tutututututu", ""+imageView!!.top+"------"+ imageView!!.left);
            val iLeft = ptCenter.x.toInt() - bl
            val iBottom = ptCenter.y.toInt() + bl
            val iRight = ptCenter.x.toInt() + bl
            val iTop = ptCenter.y.toInt() - bl
            val rllayoutParams =  imageView!! .getLayoutParams() as RelativeLayout.LayoutParams
            rllayoutParams.setMargins(iLeft, iTop, 0, 0)
             imageView!!.setLayoutParams(rllayoutParams)
        }
    }

}
